﻿using Abp.Domain.Repositories;
using Abp.Runtime.Session;
using Camc.Quality.Bom;
using Camc.Quality.BomChange.Dto;
using Camc.Quality.Materials;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.BomChange
{
   public class BomChangePublicMethodAppService : QualityAppServiceBase
    {
        private readonly IRepository<MaterialsContent, Guid> _materialsContentRepository;
        private readonly IRepository<BomDesign, Guid> _bomDesignrepository;
        private readonly IRepository<BomDesignDetail, Guid> _bomDesignDetailrepository;
        private readonly IAbpSession _abpSession;
        private readonly IRepository<BomChangeContent, Guid> _bomChangeContentrepository;

        public BomChangePublicMethodAppService(IRepository<MaterialsContent, Guid> materialsContentRepository, IRepository<BomDesign, Guid> bomDesignrepository, IRepository<BomDesignDetail, Guid> bomDesignDetailrepository, IRepository<BomChangeContent, Guid> bomChangeContenrepository)
        {
            this._materialsContentRepository = materialsContentRepository;
            this._bomDesignrepository = bomDesignrepository;
            this._bomDesignDetailrepository = bomDesignDetailrepository;
            this._bomChangeContentrepository = bomChangeContenrepository;
        }
        /// <summary>
        /// 获取型号
        /// </summary>
        /// <returns></returns>
        public async Task<List<BomChangeOhterDto>> GetModule()
        {
            var queryModuleDatas = from queryModuleData in _materialsContentRepository.GetAll().Where(t => t.MaterialCategoryId == Guid.Empty && t.MaterialProperty == 0)
                                   join queryBomDesignData in _bomDesignrepository.GetAll() on queryModuleData.Id equals queryBomDesignData.MaterialsContentId
                                   select new BomChangeOhterDto
                                   {
                                       label = queryModuleData.Model,
                                       value = queryModuleData.Id
                                   };
            if (queryModuleDatas.Count() > 0)
            {
                return queryModuleDatas.ToList();
            }
            return new List<BomChangeOhterDto>();
        }
        /// <summary>
        /// 获取发次
        /// </summary>
        /// <returns></returns>
        public async Task<List<BomChangeOhterDto>> GetSendTime()
        {
            var querySendTimeDatas = from querySendTimeData in _materialsContentRepository.GetAll().Where(t => t.MaterialCategoryId == Guid.Empty && t.MaterialProperty == 0)
                                     join queryBomDesignData in _bomDesignrepository.GetAll() on querySendTimeData.Id equals queryBomDesignData.MaterialsContentId
                                     select new BomChangeOhterDto
                                     {
                                         label = querySendTimeData.SendTime,
                                         value = queryBomDesignData.Id
                                     };
            if (querySendTimeDatas.Count() > 0)
            {
                return querySendTimeDatas.ToList();
            }
            return new List<BomChangeOhterDto>();
        }

        /// <summary>
        /// 获取芯级
        /// </summary>
        /// <returns></returns>
        public async Task<List<BomChangeOhterDto>> GetXj(BomChangeEditDto bomChangeEditDto)
        {
            //根据型号、发次 获取芯级
            var queryXjDatas = from queryXjData in _bomDesignrepository.GetAll().Where(t => t.SendTime == bomChangeEditDto.SendTime && t.Module == bomChangeEditDto.Module)
                               join queryBomDesignDetailData in _bomDesignDetailrepository.GetAll().Where(t => t.ParentId == Guid.Empty && t.LevelNum == 2) on queryXjData.Id equals queryBomDesignDetailData.BomDesignId
                               join queryMaterialsData in _materialsContentRepository.GetAll() on queryBomDesignDetailData.MaterialId equals queryMaterialsData.Id
                               select new BomChangeOhterDto
                               {
                                   label = queryMaterialsData.MaterialName,
                                   value = queryBomDesignDetailData.Id
                               };
            if (queryXjDatas.Count() > 0)
            {
                return queryXjDatas.ToList();
            }
            return new List<BomChangeOhterDto>();
        }
        /// <summary>
        /// 获取部套
        /// </summary>
        /// <returns></returns>
        public async Task<List<BomChangeOhterDto>> GetBt(BomChangeEditDto bomChangeEditDto)
        {
            //根据型号、发次 、芯级 获取部套
            var queryXjDatas = from queryXjData in _bomDesignrepository.GetAll().Where(t => t.SendTime == bomChangeEditDto.SendTime && t.Module == bomChangeEditDto.Module)
                               join queryBomDesignDetailData in _bomDesignDetailrepository.GetAll().Where(t => t.LevelNum == 3) on queryXjData.Id equals queryBomDesignDetailData.BomDesignId
                               join queryMaterialsData in _materialsContentRepository.GetAll() on queryBomDesignDetailData.MaterialId equals queryMaterialsData.Id
                               select new BomChangeOhterDto
                               {
                                   label = queryMaterialsData.MaterialName,
                                   value = queryBomDesignDetailData.Id
                               };
            if (queryXjDatas.Count() > 0)
            {
                return queryXjDatas.ToList();
            }
            return new List<BomChangeOhterDto>();
        }

    }
}
